﻿using SD.Common.PoweredByLee;
using SD.Infrastructure.EntityBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SD.ContentSystem.Domain.Entities
{
    /// <summary>
    /// 相册
    /// </summary>
    public class PhotoAlbum : AggregateRootEntity, ISortable
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected PhotoAlbum()
        {
            //初始化导航属性
            this.Photos = new HashSet<Photo>();
        }
        #endregion

        #region 01.创建相册构造器
        /// <summary>
        /// 创建相册构造器
        /// </summary>
        /// <param name="albumNo">相册编号</param>
        /// <param name="albumName">相册名称</param>
        /// <param name="author">作者</param>
        /// <param name="sort">排序</param>
        /// <param name="description">描述</param>
        public PhotoAlbum(string albumNo, string albumName, string author, int sort, string description)
            : this()
        {
            base.Number = albumNo;
            base.Name = albumName;
            this.Author = author;
            this.Sort = sort;
            this.Description = description;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #endregion

        #region # 属性

        #region 作者 —— string Author
        /// <summary>
        /// 作者
        /// </summary>
        public string Author { get; private set; }
        #endregion

        #region 封面Id —— Guid? CoverId
        /// <summary>
        /// 封面Id
        /// </summary>
        public Guid? CoverId { get; private set; }
        #endregion

        #region 相册排序 —— int Sort
        /// <summary>
        /// 相册排序
        /// </summary>
        public int Sort { get; private set; }
        #endregion

        #region 描述 —— string Description
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; private set; }
        #endregion

        #region 只读属性 - 封面路径 —— string CoverPath
        /// <summary>
        /// 只读属性 - 封面路径
        /// </summary>
        public string CoverPath
        {
            get
            {
                if (this.CoverId.HasValue)
                {
                    Photo photo = this.Photos.Single(x => x.Id == this.CoverId.Value);
                    return photo.ImagePath;
                }

                return null;
            }
        }
        #endregion

        #region 导航属性 - 相片集 —— ICollection<Photo> Photos
        /// <summary>
        /// 导航属性 - 相片集
        /// </summary>
        public virtual ICollection<Photo> Photos { get; private set; }
        #endregion

        #endregion

        #region # 方法

        #region 修改相册信息 —— void UpdateInfo(string albumName, string author...
        /// <summary>
        /// 修改相册信息
        /// </summary>
        /// <param name="albumName">相册名称</param>
        /// <param name="author">作者</param>
        /// <param name="sort">排序</param>
        /// <param name="description">描述</param>
        public void UpdateInfo(string albumName, string author, int sort, string description)
        {
            base.Name = albumName;
            this.Author = author;
            this.Sort = sort;
            this.Description = description;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #region 设置封面 —— void SetCover(Guid photoId)
        /// <summary>
        /// 设置封面
        /// </summary>
        /// <param name="photoId">相片Id</param>
        public void SetCover(Guid photoId)
        {
            //验证
            Assert.IsTrue(this.Photos.Any(x => x.Id == photoId), "相片不存在！");

            this.CoverId = photoId;
        }
        #endregion

        #region 初始化关键字 —— void InitKeywords()
        /// <summary>
        /// 初始化关键字
        /// </summary>
        private void InitKeywords()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(base.Name);
            builder.Append(this.Author);
            builder.Append(this.Description);

            base.SetKeywords(builder.ToString());
        }
        #endregion

        #endregion
    }
}
